<?php
namespace app\beautiful\controller\admin\setting;
use app\beautiful\model\setting\SettingIntegralModel;
use app\BaseController;

class Integral extends BaseController{
    public function show()
    {
        if(null === $integral_setting=SettingIntegralModel::where([['shop_id','=',$this->request->shopId]])->find()){
            $integral_setting = new SettingIntegralModel();
            $integral_setting->shop_id=$this->request->shopId;
            $integral_setting->save();
        }
        $detail=[
            'integral_exchange_balance'=>$integral_setting->integral_exchange_balance,
            'recharge_give_integral_rate'=>$integral_setting->recharge_give_integral_rate,
            'is_expire'=>$integral_setting->is_expire,
            'valid_year'=>$integral_setting->valid_year,
        ];
        return $this->success(['detail'=>$detail]);
    }

    public function save()
    {
        $integral_exchange_balance=$this->request->param('integral_exchange_balance/f',0);
        $recharge_give_integral_rate=$this->request->param('recharge_give_integral_rate/f',0);
        $is_expire=$this->request->param('is_expire/d',0);
        $valid_year=$this->request->param('valid_year/d',0);

        if($integral_exchange_balance <= 0) return $this->error('积分余额抵扣不能小于等于0');
        if($recharge_give_integral_rate < 0) return $this->error('充值送积分比例不能小于0');
        if(!in_array($is_expire,[0,1])) return $this->error('参数错误');

        if($is_expire==1){
            if($valid_year<=0)  return $this->error('过期时间不能小于等于0');
        }

        if(null === $integral_setting=SettingIntegralModel::where([['shop_id','=',$this->request->shopId]])->find()){
            $integral_setting = new SettingIntegralModel();
            $integral_setting->shop_id=$this->request->shopId;
        }

        $integral_setting->integral_exchange_balance=$integral_exchange_balance;
        $integral_setting->recharge_give_integral_rate=$recharge_give_integral_rate;
        $integral_setting->is_expire=$is_expire;
        $integral_setting->valid_year=$valid_year;
        $integral_setting->save();

        return $this->success('保存成功');
    }
}
